<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@ page import = "java.sql.PreparedStatement" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Modify Group</title>
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
	
	<script>
	
		function deleteButton(id) {
			var Flag = (document.getElementById('FLAG' + id)).innerHTML;
			
			
			if (confirm("คุณต้องการจะลบ" + groupName + "ใช่หรือไม่ ?") == true) {
				/* AJAX */
				var xmlhttp;
				
				if(window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
				else {
					// code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						window.location.reload();	
						alert("ลบข้อมูลกลุ่มวิชาสำเร็จ");////////test///////
					}
				}
				
				xmlhttp.open("POST", "curriculum_delete_group_edit.jsp?FLAG="+Flag, true);
				xmlhttp.send();
				
		    } else {

		    }
			
		}
	</script>
	

</head>
<body>

<%
	//Get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//GET PERMISSION

			String name = String.valueOf(session.getAttribute("name"));
			String surname = String.valueOf(session.getAttribute("surname"));
			String role_name = String.valueOf(session.getAttribute("curriculum_role"));
			
			String department_id = String.valueOf(session.getAttribute("department_id"));
					
			System.out.println("name : " + name);///////////////////////////////
			System.out.println("department_id : " + department_id);/////////////////////////
			
			List<String> system_name = new ArrayList<String>();
			
			Permission permission = new Permission(strUser,name);
			permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
			permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
			permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
			permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
			
			String permiss = "";
			if(permission.getCurriculumCreate()) {
				permiss = permiss + "Add";
			}
			if(permission.getCurriculumUpdate()) {
				if("".equals(permiss)) {
					permiss = permiss + "Edit";
				} else {
					permiss = permiss + " Edit";
				}
			}
			if(permission.getCurriculumRead()) {
				if("".equals(permiss)) {
					permiss = permiss + "Read";
				} else {
					permiss = permiss + " Read";
				}
			}
			if(permission.getCurriculumDelete()) {
				if("".equals(permiss)) {
					permiss = permiss + "Delete";
				} else {
					permiss = permiss + " Delete";
				}
			}
			
		///////////////////////////////////////////////////////////////////////////////////////////////////////////			
		//check permission
			if(!permission.getCurriculumCreate()){%>
				<script>alert("This user dont have PERMISSION to access this zone.");</script>
				<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
			<%}
		
	//set Database Connection
		String hostProps = "";
		String usernameProps  = "";
		String passwordProps  = "";
		String databaseProps = "";
	
		try {
			//get current path
			ServletContext servletContext = request.getSession().getServletContext();
			
			InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
			Properties props = new Properties();
			
			props.load(input);

			hostProps  = props.getProperty("host");
			usernameProps  = props.getProperty("username");
			passwordProps  = props.getProperty("password");
			databaseProps = props.getProperty("database");
		} catch (Exception e) { 
			out.println(e);  
		}
		
		System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
		System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
		System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
		System.out.println("databaseProperties : "+databaseProps);////////////////try/////////////////
		// connect database
		Connection connect = null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
			connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
					"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620");//////"&characterEncoding=tis620" Encoding Thai);
		
			if(connect != null){
				System.out.println("Database Connect Sucesses.");
			} else {
				System.out.println("Database Connect Failed.");	
			}

		} catch (Exception e) {
			out.println(e.getMessage());
			e.printStackTrace();
		}
		  
//Pass from EDIT button (Session)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//Pass from Category through session
/*		String CurId = String.valueOf(session.getAttribute("CurriculumId_category"));
		int CurriculumId_category = Integer.parseInt(CurId);  
		System.out.println("CurriculumId_category: "+CurriculumId_category);
			
		String CatId = String.valueOf(session.getAttribute("CategoryId_category"));
		int CategoryId_category = Integer.parseInt(CatId);  
		System.out.println("CategoryId_category: "+CategoryId_category);	
		 
		String Credit_cat = String.valueOf(session.getAttribute("Credit_category"));		 
		int Credit_category = Integer.parseInt(Credit_cat);
		System.out.println("Credit_category:" +Credit_category);	
				 
		String CategoryName = String.valueOf(session.getAttribute("CategoryName"));
		System.out.println("CategoryName: "+CategoryName);
		 
		String OHCE = String.valueOf(session.getAttribute("OHCE"));
		System.out.println("OHCE: "+OHCE);
		 
		String ONEC = String.valueOf(session.getAttribute("ONEC"));
		System.out.println("ONEC: "+ONEC);
*/		 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			String flag = request.getParameter("flag");
			int CurriculumIdOLD = Integer.parseInt(request.getParameter("CurriculumId"));
			int CategoryIdOLD = Integer.parseInt(request.getParameter("CategoryId"));
			String categoryName = new String(request.getParameter("categoryName").getBytes("ISO8859_1"),"UTF-8");
		 	int Credit_categoryOLD = Integer.parseInt(request.getParameter("Credit_category"));		
			String OHCE = request.getParameter("OHCE");
		 	String ONEC = request.getParameter("ONEC");
		 	String EngApp = request.getParameter("EngApp");	
		 	String C_Group = request.getParameter("GroupId");	
		 	
			System.out.println("CurriculumIdOLD:   "+CurriculumIdOLD);
			System.out.println("CategoryIdOLD:   "+CategoryIdOLD);			
			System.out.println("Credit_categoryOLD:   "+Credit_categoryOLD);		
			System.out.println("categoryName:    "+categoryName);
			System.out.println("OHCE:    "+OHCE);
			System.out.println("ONEC:    "+ONEC);
			System.out.println("EngApp:    "+EngApp);
			
			int tempCurriculumIdOLD = CurriculumIdOLD;
			int tempCategoryIdOLD = CategoryIdOLD;
			String tempcategoryName = categoryName;
			int  tempCredit_categoryOLD = Credit_categoryOLD;
			String tempOHCE = OHCE;
			String tempONEC = ONEC;
			String tempEngApp = EngApp;
			
			int temp = 0;
			
/////////////////////////////////////////////////////////////////////////////////////////////////

if(request.getParameter("checkerCancel").equals("Cancel")){	
	//delete curriculum in database
	try {			   
		connect.createStatement().executeUpdate("DELETE FROM `course` "
		+" WHERE group_id = '"+ C_Group + "'");
		
		System.out.println("delete sucess");////////////try//////////
	} catch (SQLException e) {
		e.printStackTrace();
	}

}				
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
/*Find Category in DB*/
			List<Category> category2 = new ArrayList<Category>();
			List<Group> group2 = new ArrayList<Group>();
			try{
				ResultSet rs_cat2 = connect.createStatement().executeQuery("SELECT `group_name`, C.category_id, G.category_id, `category_name`, C.curriculum_id, G.curriculum_id, G.credit, C.credit, `group_id` "
									+ " FROM subject_category C, subject_group G " 
									+ " WHERE C.category_id=G.category_id AND G.curriculum_id=" + tempCurriculumIdOLD +" AND G.category_id=" + tempCategoryIdOLD +" "
									+ " ORDER BY group_id");
				
				while(rs_cat2.next()) {
					Category cate2 = new Category();
					
					cate2.setCategoryName(rs_cat2.getString("category_name"));
					cate2.setCurriculumId(rs_cat2.getInt("C.curriculum_id"));				
					cate2.setCredit(rs_cat2.getInt("C.credit"));		
					cate2.setCategoryId(rs_cat2.getInt("C.category_id"));
					
					category2.add(cate2);
					
					Group grou2 = new Group();
					
					grou2.setGroupName(rs_cat2.getString("group_name"));
					grou2.setCredit(rs_cat2.getInt("G.credit"));
					grou2.setGroupId(rs_cat2.getInt("group_id"));
					
					group2.add(grou2);
				}
				
			}catch (SQLException e) {
				e.printStackTrace();
			}
			
				
			for(int tempi= 0;tempi<group2.size();tempi++){
					temp = temp +group2.get(tempi).getCredit();
					System.out.println("Total Current Credit:     "+ temp);
			}
				
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	



//if add new info do this !	
			if(request.getParameter("checker").equals("ADD")){
				
				request.setCharacterEncoding("UTF-8");
				//Temp
				int tempCredit = temp;		
				String tempGroupName = new String(request.getParameter("nameGroup").getBytes("ISO8859_1"),"UTF-8");
			 	String tempCreditGroup =request.getParameter("Credit_group");;		
			 	
			 	if(tempGroupName.equals("") || tempCreditGroup.equals("")){
			 		if(tempGroupName.equals("") ){%>
					<script>alert("กรุณาใส่ชื่อกลุ่มวิชา");</script>
					<%}
						else if(tempCreditGroup.equals("") ){%>
						<script>alert("กรุณาใส่จำนวนหน่วยกิต");</script>
					<%}
			 		
			 	}else{
					 String GroupName = new String(request.getParameter("nameGroup").getBytes("ISO8859_1"),"UTF-8");
					 String CategoryId = request.getParameter("CategoryId");
					 System.out.println("/////////////////////////////////////////////////////////////");
					 System.out.println("Total Current CategoryId:     "+ CategoryId);
					 int Credit = Integer.parseInt(request.getParameter("Credit_group"));
					 
					int total = tempCredit+Credit;	
					temp = total;
					
					if(total > tempCredit_categoryOLD){	
						temp=tempCredit;
					%>	
						<script>alert("หน่วยกิตของกลุ่มวิชา_ไม่สามารถมากกว่าหน่วยกิตของหมวดวิชาได้ \n\nกรุณาใส่ใหม่อีกครั้ง");</script>
		<%				}else{	
						
							try {			   
								connect.createStatement().executeUpdate("INSERT INTO `subject_group`( `group_name`, `curriculum_id`, `category_id`, `credit`) "
												+ " VALUES ('"+ GroupName +"','"+ tempCurriculumIdOLD +"','"+ tempCategoryIdOLD +"','"+ Credit +"')" );
								
								Group tempAdd = new Group();
								
								tempAdd.setGroupName(GroupName);
								tempAdd.setCategoryId(tempCategoryIdOLD);
								tempAdd.setCurriculumId(tempCurriculumIdOLD);
								tempAdd.setCredit(Credit);
								
								group2.add(tempAdd);
								
								String sql_subject_group = "INSERT INTO `subject_group`( `group_name`, `curriculum_id`, `category_id`, `credit`) "
										+ " VALUES ('"+ GroupName +"','"+ tempCurriculumIdOLD +"','"+ tempCategoryIdOLD +"','"+ Credit +"')";
								
								/* Log file */
								String log_user = strUser+" add subject_group";
								Log.writeUserFile(log_user);
								Log.writeUserFile(sql_subject_group);
								
								
						
		%>							
								<script language="javascript"> alert("บันทึกข้อมูลสำเร็จ");</script>	
		<%			
		
		
								} catch (SQLException e) {
									e.printStackTrace();
								}
						}
			 	}
	}
			
//////////////////////////////////////////////////////////////////////////////////////////////////			
			
			/*Find Category in DB*/
			List<Category> category = new ArrayList<Category>();
			List<Group> group = new ArrayList<Group>();
			try{
				ResultSet rs_cat = connect.createStatement().executeQuery("SELECT `group_name`, C.category_id, G.category_id, `category_name`, C.curriculum_id, G.curriculum_id, G.credit, C.credit, `group_id` "
									+ " FROM subject_category C, subject_group G " 
									+ " WHERE C.category_id=G.category_id AND G.curriculum_id=" + tempCurriculumIdOLD +" AND G.category_id=" + tempCategoryIdOLD +" "
									+ " ORDER BY group_id");
				
				while(rs_cat.next()) {
					Category cate = new Category();
					
					cate.setCategoryName(rs_cat.getString("category_name"));
					cate.setCurriculumId(rs_cat.getInt("C.curriculum_id"));				
					cate.setCredit(rs_cat.getInt("C.credit"));		
					cate.setCategoryId(rs_cat.getInt("C.category_id"));
					
					category.add(cate);
					
					Group grou = new Group();
					
					grou.setGroupName(rs_cat.getString("group_name"));
					grou.setCredit(rs_cat.getInt("G.credit"));
					grou.setGroupId(rs_cat.getInt("group_id"));
					
					group.add(grou);
				}
				
			}catch (SQLException e) {
				e.printStackTrace();
			}
			
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
			

////////////////////////////QUERY CURRENT CURRICULUM /////////////////////////////////
/*Find Category in DB*/
			List<Category> category4 = new ArrayList<Category>();
			List<Group> group4 = new ArrayList<Group>();
			try{
				ResultSet rs_cat4 = connect.createStatement().executeQuery("SELECT `group_name`, C.category_id, G.category_id, `category_name`, C.curriculum_id, G.curriculum_id, G.credit, C.credit, `group_id` "
									+ " FROM subject_category C, subject_group G " 
									+ " WHERE C.category_id=G.category_id AND G.curriculum_id=" + tempCurriculumIdOLD +" AND G.category_id=" + tempCategoryIdOLD +" "
									+ " ORDER BY group_id");
				
				while(rs_cat4.next()) {
					Category cate = new Category();
					
					cate.setCategoryName(rs_cat4.getString("category_name"));
					cate.setCurriculumId(rs_cat4.getInt("C.curriculum_id"));				
					cate.setCredit(rs_cat4.getInt("C.credit"));		
					cate.setCategoryId(rs_cat4.getInt("C.category_id"));
					
					category4.add(cate);
					
					Group grou = new Group();
					
					grou.setGroupName(rs_cat4.getString("group_name"));
					grou.setCredit(rs_cat4.getInt("G.credit"));
					grou.setGroupId(rs_cat4.getInt("group_id"));
					
					group4.add(grou);
				}
				
			}catch (SQLException e) {
				e.printStackTrace();
			}
%>
	 <div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right" ><label><font size="4" color="#FFFFFF">  username : <%=strUser %>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>							
				</tr>
			</table>
    
				<div id="main-content-2">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<label><font size="6">ตอนนี้คุณอยู่ในหมวด <%=tempcategoryName %></font></label>
				</div>
				
				
				<div id="main-content-ac" align="center" >
					<br/>
					<table border="0">
					<form id="form" name="form" method="post" action="curriculum_modified_group.jsp">	
						
						<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=tempCurriculumIdOLD%>" />
						<input type="hidden" id="CategoryId" name="CategoryId" value="<%=tempCategoryIdOLD%>" />
						<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" />
						<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
						<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
						<input type="hidden" id="ONEC" name="ONEC" value="<%=tempONEC%>" />						
						<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />
						<input type="hidden" id="flag" name="flag" value="0" />	
						<input type="hidden" id="checker" name="checker" value="ADD" />	
						<input type="hidden" id="checkerCancel" name="checkerCancel" value="none" />
												
						<tr style="font-size:24px">
							<td height="50">
								<font size="5"><strong>ชื่อกลุ่มวิชา</strong></font>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">
								<input name="nameGroup" type="text" required style="font-size:18px" />
								
								
							</td>
							
						</tr>
						
						<tr style="font-size:24px">
							<td  height="50">
								<font size="5"><strong>จำนวนหน่วยกิต</strong></font>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">
								<input name="Credit_group" type="text" required style="font-size:18px" />
							</td>
						</tr>
							
						<tr style="font-size:24px">
							<td width="400" height="50">
								<font size="3" color="#FF0000">หมายเหตุ: * คือ ข้อมูลสำคัญ จำเป็นต้องกรอก</font>
							</td>
							<td height="50">
								<input type="submit" value="ADD" name="ADD" class="buttonStyle"/>
							</td>
						</tr>
													
				</form>								
					</table>
				</div>	
				
					<div id="main-content-ac" >						
						<br/>
						<center>
						<div class="CSSTableGenerator">							
								<table border="1">
									<tr style="font-size:22px">
									   	<td>&nbsp;&nbsp;&nbsp;</td>
									    <td><center><strong>ชื่อกลุ่มวิชา</strong></center></td>
										<td><center><strong>หมวดวิชา</strong></center></td>
										<td><center><strong>หน่วยกิต</strong></center></td>																						
										<td>เพิ่มรายวิชา</td>
										<td>ลบกลุ่มวิชา</td>
										<td>แก้ไขกลุ่มวิชา</td>
									  </tr>
									  
			    <%// TEST EDIT				    
					int groupCount = 1;
			    	int i=0;
			    	for(Group gro : group4 ) {	
				%>
									<tr style="font-size:18px" id="Curriculum<%=groupCount%>">
										<form id="form<%=groupCount%>" name="form<%=groupCount%>" method="post" action="curriculum_modified_course.jsp">
												
												<input type="hidden" id="checker2" name="checker2" value="none" />
												<input type="hidden" id="checkerCancel" name="checkerCancel" value="none" />
												
												<input type="hidden" id="checker2" name="checker2" value="none" />
												<input type="hidden" id="flag" name="flag" value="1" />
												<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=category.get(0).getCurriculumId()%>" />
												<input type="hidden" id="CategoryId" name="CategoryId" value="<%=category.get(0).getCategoryId()%>" />	
												<input type="hidden" id="GroupId" name="GroupId" value="<%=gro.getGroupId()%>"/>
												<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" /> 
												<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
												<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
												<input type="hidden" id="ONEC" name="ONEC" value="<%=tempOHCE%>" />
												<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />
												
												
												<td id="groupCount" name="groupCount" align="center">
													<%=groupCount%>
												</td>
												
												<input type="hidden" id="GroupName" name="GroupName" value="<%=gro.getGroupName()%>" />  
												<td id="GroupName<%=groupCount%>" name="GroupName<%=groupCount%>" align="center">
													<%=gro.getGroupName()%>
												</td>
												
												<input type="hidden" id="CategoryId" name="CategoryId" value="<%=gro.getCategoryId()%>" />
												<td id="CategoryId<%=groupCount%>" name="CategoryId<%=groupCount%>" align="center">
													<%=category.get(0).getCategoryName()%>
												</td>
												
												<input type="hidden" id="Credit_group" name="Credit_group" value="<%=gro.getCredit()%>" /> 
												<td id="Credit_group<%=groupCount%>" name="Credit_group<%=groupCount%>" align="center">
													<%=gro.getCredit()%>
												</td>	
												<td><center><span title="เพิ่มข้อมูล"><input type="image" src="image/add_icon.png" height="30px" width="30px" /></span></center></td>
												
											</form>	
											
											<form id="form<%=groupCount%>" name="form<%=groupCount%>" method="post" action="curriculum_delete_group_modified.jsp">	
											
												<input type="hidden" id="flag" name="flag" value="confirm" />
												<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=category.get(0).getCurriculumId()%>" /> 											
												<input type="hidden" id="CategoryId" name="CategoryId" value="<%=category.get(0).getCategoryId()%>" />	
												<input type="hidden" id="GroupId" name="GroupId" value="<%=gro.getGroupId()%>" />
												<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" /> 
												<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
												<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
												<input type="hidden" id="ONEC" name="ONEC" value="<%=tempOHCE%>" />

												<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />
												<input type="hidden" id="GroupName" name="GroupName" value="<%=gro.getGroupName()%>" /> 
												<input type="hidden" id="Credit_group" name="Credit_group" value="<%=gro.getCredit()%>" />
												
												<td><center><span title="ลบข้อมูล"><input type="image" src="image/delete_icon.png" height="30px" width="30px"/></span></center></td>
											</form>
											
											<form id="form<%=groupCount%>" name="form<%=groupCount%>" method="post" action="curriculum_group_modified_info.jsp">
												<input type="hidden" id="flag" name="flag" value="confirm" />
												<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=category.get(0).getCurriculumId()%>" /> 											
												<input type="hidden" id="CategoryId" name="CategoryId" value="<%=category.get(0).getCategoryId()%>" />	
												<input type="hidden" id="checker" name="checker" value="none" />
												<input type="hidden" id="GroupId" name="GroupId" value="<%=gro.getGroupId()%>" />
												<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" /> 
												<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
												<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
												<input type="hidden" id="ONEC" name="ONEC" value="<%=tempOHCE%>" />
												<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />
												
												<input type="hidden" id="GroupName" name="GroupName" value="<%=gro.getGroupName()%>" /> 
												<input type="hidden" id="Credit_group" name="Credit_group" value="<%=gro.getCredit()%>" />												
												<input type="hidden" id="checkButton" name="checkButton" value="Edit" />
												
												<td><center><span title="แก้ไขข้อมูล"><input type="image" src="image/edit_icon.png" height="30px" width="30px"/></span></center></td>	
											</form>	
										
										</tr>
				
				<%
						i++;
						groupCount++;

					}
			    	
			    
				%>	
				</table>
				
			</div>
				<br><br>			
				<table>
					<tr style="font-size:20px">
						<td height="50" width = "230">จำนวนหน่วยกิตรวมสูงสุดของหมวดวิชา =   <%= tempCredit_categoryOLD%></td>
						<td height="50" width = "230">จำนวนหน่วยกิตรวมของทุกกลุ่มวิชา =   <%= temp%></td>
					</tr>
				</table>
				
				<br><br><font size="4" color="red">คำเตือน: ถ้ากดปุ่ม cancel ข้อมูลจะไม่ได้รับการบันทึก !</font><br><br>
				
			<table>
				<form id="form5" name="form5" method="post" action="">
					<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=tempCurriculumIdOLD%>" />
					<input type="hidden" id="CategoryId" name="CategoryId" value="<%=tempCategoryIdOLD%>" />
					<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" />
					<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
					<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
					<input type="hidden" id="ONEC" name="ONEC" value="<%=tempONEC%>" />						
					<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />
					<input type="hidden" id="flag" name="flag" value="completed" />
					<input type="hidden" id="checker" name="checker" value="none" />			
					<input type="hidden" id="checkerCancel" name="checkerCancel" value="none" />
				<tr>
							<td height="50" width = "230">										
								<center><input type="submit" value="save" name="save" class="buttonStyle"/></center>
							</td>
				</form>				
						<form id="form6" name="form6" method="post" action="">	
							<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=tempCurriculumIdOLD%>" />
							<input type="hidden" id="CategoryId" name="CategoryId" value="<%=tempCategoryIdOLD%>" />
							<input type="hidden" id="categoryName" name="categoryName" value="<%=tempcategoryName%>" />
							<input type="hidden" id="Credit_category" name="Credit_category" value="<%=tempCredit_categoryOLD%>" />
							<input type="hidden" id="OHCE" name="OHCE" value="<%=tempOHCE%>" />
							<input type="hidden" id="ONEC" name="ONEC" value="<%=tempONEC%>" />	
							<input type="hidden" id="EngApp" name="EngApp" value="<%=tempEngApp%>" />						
							<input type="hidden" id="flag" name="flag" value="completed" />	
							<input type="hidden" id="checker" name="checker" value="none" />		
							<input type="hidden" id="checkerCancel" name="checkerCancel" value="none" />
							
							<td height="50" width = "230">
								<center><input type="submit" value="cancel" name="cancel2" class="buttonStyle"/></center>
							</td>
				</tr>
						</form>		
			</table>	
				

				
					</center>	
					<br><br>				
				</div>
				<div style="clear: both;"></div>
					<div id="footer"></div>
		</div>	
					<div style="clear: both;"></div>	
    </div>

    
<%
		String checkButton = request.getParameter("add");
		String checkButton2 = request.getParameter("save");
		String checkButton3 = request.getParameter("cancel2");		
		String checkButton4 = request.getParameter("delete");	
		
		////////////////////////////////////////////////////////////////////////////////
		System.out.println("Button:  "+checkButton);
		System.out.println("Button:  "+checkButton2);
		System.out.println("Button:  "+checkButton3);
		System.out.println("Button:  "+checkButton4);
		
		// check save button is pressed
		if("add".equals(checkButton)) {	
			
				request.setCharacterEncoding("UTF-8");
				//Temp
				int tempCredit = temp;		
				String tempGroupName = new String(request.getParameter("nameGroup").getBytes("ISO8859_1"),"UTF-8");
			 	String tempCreditGroup =request.getParameter("Credit_group");;		
			 	
			 	if(tempGroupName.equals("") || tempCreditGroup.equals("")){
			 		if(tempGroupName.equals("") ){%>
					<script>alert("กรุณาใส่ชื่อกลุ่มวิชา");</script>
					<%}
  					else if(tempCreditGroup.equals("") ){%>
						<script>alert("กรุณาใส่จำนวนหน่วยกิต");</script>
					<%}
			 		
			 	}else{
					 String GroupName = new String(request.getParameter("nameGroup").getBytes("ISO8859_1"),"UTF-8");
					 String CategoryId = request.getParameter("CategoryId");
					 System.out.println("/////////////////////////////////////////////////////////////");
					 System.out.println("Total Current CategoryId:     "+ CategoryId);
					 int Credit = Integer.parseInt(request.getParameter("Credit_group"));
					 
					int total = tempCredit+Credit;	
					
					if(Credit > tempCredit_categoryOLD || total > tempCredit_categoryOLD){	%>	
						<script>alert("หน่วยกิตของกลุ่มวิชา_ไม่สามารถมากกว่าหน่วยกิตของหมวดวิชาได้ \n\nกรุณาใส่ใหม่อีกครั้ง");</script>
	<%				}else{	
						
							try {			   
								connect.createStatement().executeUpdate("INSERT INTO `subject_group`( `group_name`, `curriculum_id`, `category_id`, `credit`) "
												+ " VALUES ('"+ GroupName +"','"+ tempCurriculumIdOLD +"','"+ tempCategoryIdOLD +"','"+ Credit +"')" );
								
								String sql_subject_group = "INSERT INTO `subject_group`( `group_name`, `curriculum_id`, `category_id`, `credit`) "
										+ " VALUES ('"+ GroupName +"','"+ tempCurriculumIdOLD +"','"+ tempCategoryIdOLD +"','"+ Credit +"')";
								
								/* Log file */
								String log_user = strUser+" add subject_group";
								Log.writeUserFile(log_user);
								Log.writeUserFile(sql_subject_group);
								
								
								
	%>							
							<script language="javascript"> alert("บันทึกข้อมูลสำเร็จ\nข้อมูลที่เพิ่มจะแสดงผลหลังจากกดปุ่ม Refresh");</script>		
	<%			
	
	
								} catch (SQLException e) {
									e.printStackTrace();
								}
						}
			 	}
		}else if("save".equals(checkButton2)){
			//request.setCharacterEncoding("UTF-8");
			if(temp < tempCredit_categoryOLD){	
					%><script>alert("กรุณาใส่หน่วยกิตให้ครบตามที่กำหนดไว้");</script><%
			}if(temp > tempCredit_categoryOLD){	
				%><script>alert("หน่วยกิตของกลุ่มวิชาไม่สามารถมากกว่า_หน่วยกิตของหมวดวิชาได้\nกรุณาแก้ไขหรือลบกลุ่มวิชาอื่นๆ");</script><%
			}if(temp == tempCredit_categoryOLD){	
%>
					<%session.setAttribute("Credit_group_cat",tempCredit_categoryOLD);%>
					<%session.setAttribute("button1",5);%>
					
					<%System.out.println("Phase 4dfg Success with Credit_group :"+ tempCredit_categoryOLD);%>
					<%System.out.println("Phase 4dfg Success with button1 :"+ 5);%>
					
					<script language="javascript"> alert("บันทึกข้อมูลกลุ่มวิชาสำเร็จ");</script>							
					<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_category_modified.jsp">
			<%}
		}else if("cancel".equals(checkButton3)) {	
			
			System.out.println("DELETE BUTTON");////////////try//////////
			request.setCharacterEncoding("UTF-8");
			try {			   
				connect.createStatement().executeUpdate("DELETE FROM `subject_group` "
						+" WHERE category_id = '"+ CategoryIdOLD + "'");
				
				String sql_subject_group = "DELETE FROM `subject_group` "
						+" WHERE category_id = '"+ CategoryIdOLD + "'";
				
				/* Log file */
				String log_user = strUser+" delete subject_group";
				Log.writeUserFile(log_user);
				Log.writeUserFile(sql_subject_group);
				
						
				System.out.println("delete sucess");////////////try//////////
%>			
				<script>alert("กดปุ่ม  Cancel กลุ่มวิชาจะถูกลบทั้งหมด")</script>
				<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_category_modified.jsp">
<%
				
				} catch (SQLException e) {
					e.printStackTrace();
				}

	}
		
		connect.close();
	
%> 

</body>
</html>